﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class SoHieuTaiKhoanBOCollection : Csla.BusinessListBase<SoHieuTaiKhoanBOCollection, SoHieuTaiKhoanBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            SoHieuTaiKhoanBO _SoHieuTaiKhoanBO = SoHieuTaiKhoanBO.NewChildSoHieuTaiKhoanBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_SoHieuTaiKhoanBO));
            Add(_SoHieuTaiKhoanBO);
            return _SoHieuTaiKhoanBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private SoHieuTaiKhoanBOCollection()
        {
        }

        public static SoHieuTaiKhoanBOCollection NewSoHieuTaiKhoanBOCollection()
        {
            return new SoHieuTaiKhoanBOCollection();
        }

        public static SoHieuTaiKhoanBO NewSoHieuTaiKhoanBO()
        {
            return SoHieuTaiKhoanBO.NewChildSoHieuTaiKhoanBO();
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection()
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", "");
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection(string where)
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }

        public static SoHieuTaiKhoanBOCollection GetSoHieuTaiKhoanBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<SoHieuTaiKhoanBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("SoHieuTaiKhoanBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    SoHieuTaiKhoanBO objSoHieuTaiKhoanBO = SoHieuTaiKhoanBO.GetChildSoHieuTaiKhoanBO(dr);
                    this.Add(objSoHieuTaiKhoanBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {

        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public SoHieuTaiKhoanBO GetSoHieuTaiKhoanBO(int _TaiKhoanKTID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].TaiKhoanKTID == _TaiKhoanKTID)
                    return this[i];

            return null;
        }

        public SoHieuTaiKhoanBO GetSoHieuTaiKhoanBO(string _StrSoHieu)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].StrSoHieu == _StrSoHieu)
                    return this[i];

            return null;
        }

        public void Remove(int TaiKhoanKTID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].TaiKhoanKTID == TaiKhoanKTID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }

        public void Sort()
        {
            Sort(0, this.Count - 1);
        }

        private void Sort(int beg, int end)
        {
            if (end <= beg)
                return;
            else
            {
                int pivot = GetPivotPoint(beg, end);
                if (pivot > beg)
                    Sort(beg, pivot - 1);
                if (pivot < end)
                    Sort(pivot + 1, end);
            }
        }

        private int GetPivotPoint(int begPoint, int endPoint)
        {
            int pivot = begPoint;
            int m = begPoint + 1;
            int n = endPoint;
            string sttPivot = this[pivot].SortStr;
            string sttM = this[m].SortStr;
            string sttN = this[n].SortStr;
            SoHieuTaiKhoanBO temp = null;

            while ((m < endPoint) && String.Compare(sttPivot, sttM) >= 0)
            {
                m++;
                sttM = this[m].SortStr;
            }

            while ((n > begPoint) && String.Compare(sttPivot, sttN) <= 0)
            {
                n--;
                sttN = this[n].SortStr;
            }

            while (m < n)
            {
                temp = this[m].Clone();
                this[m] = this[n];
                this[n] = temp;

                sttM = this[m].SortStr;
                sttN = this[n].SortStr;

                while (m < endPoint && String.Compare(sttPivot, sttM) >= 0)
                {
                    m++;
                    sttM = this[m].SortStr;
                }

                while (n > begPoint && String.Compare(sttPivot, sttN) <= 0)
                {
                    n--;
                    sttN = this[n].SortStr;
                }
            }

            if (pivot != n)
            {
                temp = this[n].Clone();
                this[n] = this[pivot];
                this[pivot] = temp;
            }

            return n;
        }
        #endregion Business Method
        //==========================================================================
    }
}
